package com.example.broadcastapplication;

import android.Manifest;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import pub.devrel.easypermissions.EasyPermissions;

public class MainActivity extends AppCompatActivity {

    private OutgingCallReceiver receiver;//广播接收者
    private StudyReceiver studyReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //动态注册广播，接收自定义的那个广播="study_Android"
        studyReceiver = new StudyReceiver();
        IntentFilter intentFilter=new IntentFilter();
        intentFilter.addAction("study_Android");
        registerReceiver(studyReceiver,intentFilter);



        //申请处理外拨电话的权限
        String[]perms={Manifest.permission.PROCESS_OUTGOING_CALLS};
        if (!EasyPermissions.hasPermissions(this,perms)){
            EasyPermissions.requestPermissions(this,"outgoingcall ",11,perms);
        }


        Button btnSend= (Button) findViewById(R.id.btn_send);
        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //发送一个自定义的广播
                Intent intent=new Intent( );
                //设置intent的action
                intent.setAction("study_Android");
                //发送广播
                sendBroadcast(intent);
            }
        });
        /*


        //注册广播接收者接收的广播类型
        //广播接收者
        receiver = new OutgingCallReceiver();
        //intentfilter
        IntentFilter intentFilter=new IntentFilter();
        intentFilter.addAction(Intent.ACTION_NEW_OUTGOING_CALL);

        //注册
        // 参数1=广播接收者对象，参数2-intentFilter
        registerReceiver(receiver,intentFilter);
        */
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //解除注册
        //unregisterReceiver(receiver);
        unregisterReceiver(studyReceiver);
    }
}
